<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>LNMP- php编译安装 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>LNMP- php编译安装</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/LNMP/">LNMP</a> <a class="article__tag-link" href="/tags/Php/">Php</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;首先针对 nginx 的 php 安装和针对 apache 的 php 安装是有区别的，因为 nginx 中的 php 是以 fastcgi 的方式结合 nginx 的。可以理解为 nginx 代理了 php 的 fastcgi ，而 apache 是把 php 作为子机的模块来调用的。<a href="http://www.php.net/downloads.php" target="_blank" rel="external">php官方下载地址</a></p>
<h2 id="1-下载-php-源码包"><a href="#1-下载-php-源码包" class="headerlink" title="1.下载 php 源码包"></a>1.下载 php 源码包</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@lnmp ~]<span class="comment"># cd /usr/local/src</span></div><div class="line">[root@lnmp src]<span class="comment"># wget http://aul.php.net/distributions/php-5.4.44.tar.bz2</span></div></pre></td></tr></table></figure>
<h2 id="2-解压源码包"><a href="#2-解压源码包" class="headerlink" title="2.解压源码包"></a>2.解压源码包</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@lnmp src]<span class="comment"># tar jxf php.5.4.44.tar.bz2</span></div></pre></td></tr></table></figure>
<h2 id="3-创建相关帐号"><a href="#3-创建相关帐号" class="headerlink" title="3.创建相关帐号"></a>3.创建相关帐号</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@lnmp src]<span class="comment"># useradd -s /sbin/nologin php-fpm</span></div></pre></td></tr></table></figure>
<h2 id="4-依赖包安装"><a href="#4-依赖包安装" class="headerlink" title="4.依赖包安装"></a>4.依赖包安装</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">yum install -y bzip2-devel curl-devel db4-devel libjpeg-devel libpng-devel libXpm-devel gmp-devel libc-client-devel openldap-devel unixODBC-devel postgresql-devel sqlite-devel aspell-devel net-snmp-devel libxslt-devel libxml2-devel pcre-devel mysql-devel unixODBC-devel postgresql-devel pspell-devel net-snmp-devel freetype-devel libtomcrypt-devel.x86_64 php-mcrypt libmcrypt libmcrypt-devel</div></pre></td></tr></table></figure>
<h2 id="5-配置编译参数"><a href="#5-配置编译参数" class="headerlink" title="5.配置编译参数"></a>5.配置编译参数</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div></pre></td><td class="code"><pre><div class="line">[root@lnmp src]<span class="comment"># cd php-5.4.44</span></div><div class="line">[root@lnmp php-5.4.44]<span class="comment"># ./configure \</span></div><div class="line">--prefix=/usr/<span class="built_in">local</span>/php \</div><div class="line">--with-config-file-path=/usr/<span class="built_in">local</span>/php/etc \</div><div class="line">--<span class="built_in">enable</span>-fpm \</div><div class="line">--with-fpm-user=php-fpm \</div><div class="line">--with-fpm-group=php-fpm \</div><div class="line">--with-mysql=/usr/<span class="built_in">local</span>/mysql \</div><div class="line">--with-mysql-sock=/tmp/mysql.sock \</div><div class="line">--with-libxml-dir \</div><div class="line">--with-gd \</div><div class="line">--with-jpeg-dir \</div><div class="line">--with-png-dir \</div><div class="line">--with-freetype-dir \</div><div class="line">--with-iconv-dir \</div><div class="line">--with-zlib-dir \</div><div class="line">--with-mcrypt \</div><div class="line">--<span class="built_in">enable</span>-soap \</div><div class="line">--<span class="built_in">enable</span>-gd-native-ttf \</div><div class="line">--<span class="built_in">enable</span>-ftp \</div><div class="line">--<span class="built_in">enable</span>-mbstring \</div><div class="line">--<span class="built_in">enable</span>-exif \</div><div class="line">--<span class="built_in">disable</span>-ipv6 \</div><div class="line">--with-pear \</div><div class="line">--with-curl \</div><div class="line">--with-openssl</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在该过程中，若出现和 lamp 安装 php 的错误一样的错误参照 lamp 的方法来解决，若出现如下错误，参考下面解决办法：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;错误信息：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">configure: error: Please reinstall the libcurl distribution -</div><div class="line"> easy.h should be <span class="keyword">in</span> &lt;curl-dir&gt;/include/curl/</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;解决办法：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">yum install -y libcurl-devel</div></pre></td></tr></table></figure>
<h2 id="6-编译-php"><a href="#6-编译-php" class="headerlink" title="6.编译 php"></a>6.编译 php</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@lnmp php-5.4.44]<span class="comment"># make</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在这一步，通常会遇到一些错误：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">/usr/bin/ld: cannot find -lltdl</div><div class="line">collect2: ld returned 1 <span class="built_in">exit</span> status</div><div class="line">make: *** [sapi/fpm/php-fpm] 错误 1</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;解决办法：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">yum install -y libtool-ltdl-devel</div></pre></td></tr></table></figure>
<h2 id="7-安装-php"><a href="#7-安装-php" class="headerlink" title="7.安装 php"></a>7.安装 php</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@lnmp php-5.4.44]<span class="comment"># make install</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以上每一步骤，如果没有完全执行正确，那么下一步是无法进行的，使用 echo $? 看结果是否为 “0”，如果不是，就是没有执行正确。</p>
<h2 id="8-修改配置文件"><a href="#8-修改配置文件" class="headerlink" title="8.修改配置文件"></a>8.修改配置文件</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">cp php.ini-production /usr/<span class="built_in">local</span>/php/etc/php.ini</div><div class="line">vim /usr/<span class="built_in">local</span>/php/etc/php-fpm.conf</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;添加如下内容：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">[global]</div><div class="line">pid = /usr/<span class="built_in">local</span>/php/var/run/php-fpm.pid</div><div class="line">error_log = /usr/<span class="built_in">local</span>/php/var/<span class="built_in">log</span>/php-fpm.log</div><div class="line">[www]</div><div class="line">listen = /tmp/php-fcgi.sock</div><div class="line">user = php-fpm</div><div class="line">group = php-fpm</div><div class="line">pm = dynamic</div><div class="line">pm.max_children = 50</div><div class="line">pm.start_servers = 20</div><div class="line">pm.min_spare_servers = 5</div><div class="line">pm.max_spare_servers = 35</div><div class="line">pm.max_requests = 500</div><div class="line">rlimit_files = 1024</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;保存配置文件后，检验配置是否正确的方法为：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">/usr/<span class="built_in">local</span>/php/sbin/php-fpm -t</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果出现诸如 “test is successful” 字样，说明配置没有问题。</p>
<h2 id="9-启动-php-fpm"><a href="#9-启动-php-fpm" class="headerlink" title="9.启动 php-fpm"></a>9.启动 php-fpm</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;首先要拷贝一个启动脚本到 /etc/init.d 下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">cp /usr/<span class="built_in">local</span>/src/php-5.4.44/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;更改权限为755</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">chmod 755 /etc/init.d/php-fpm</div><div class="line">service php-fpm start</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果要它开机启动，执行：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">chkconfig php-fpm on</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;检测是否成功：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">ps aux |grep php-fpm</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;看看是不是有很多进程</p>

	

	
		<span class="different-posts"><a href="/2017/10/12/Php/13. LNMP- php编译安装/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
